package cuongnh.project.japanesediary.vocabulary.ui.view.action.menucontribution.category;

import java.util.Set;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.ui.IViewActionDelegate;
import org.eclipse.ui.IViewPart;

import cuongnh.project.japanesediary.vocabulary.common.CommonServices;
import cuongnh.project.japanesediary.vocabulary.model.Category;
import cuongnh.project.japanesediary.vocabulary.ui.view.CategoryListView;

public class RemoveCategoryAction implements IViewActionDelegate {

	public static final String ID = "cuongnh.project.japanesediary.vocabulary.action.RemoveCategory";

	protected IViewPart m_view;

	private CommonServices commonServices;

	private Category category;

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.IViewActionDelegate#init(org.eclipse.ui.IViewPart)
	 */
	@Override
	public void init(IViewPart view) {
		m_view = view;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
	 */
	@Override
	public void run(IAction action) {
		// 履歴ビュー更新
		CategoryListView view = (CategoryListView) m_view
				.getAdapter(CategoryListView.class);
		commonServices = view.getCommonServices();
		category = view.getSelectedCategory();
		if (category != null) {
			boolean confirm = MessageDialog
					.openConfirm(view.getSite().getShell(),
							"Deleting confirmation!",
							"Are you sure you want to delete this Category and its children categories?");
			if (confirm) {
				removeCategory(category);
				Category parentCategory = category.getParentCategory();
				if (parentCategory != null) {
					parentCategory.getSubCategories().remove(category);
					view.refresh();
					view.getTblvCategoryList().setSelection(
							new StructuredSelection(category
									.getParentCategory()));
				}else{					
					view.refresh();
					view.getTblvCategoryList().setSelection(StructuredSelection.EMPTY);
				}				
			}
		}
	}

	private void removeCategory(Category category) {
		category = commonServices.getCategoryDao().getCategoryWithAllLazies(
				category.getCategoryId());
		Set<Category> subCategories = category.getSubCategories();
		if (!subCategories.isEmpty()) {
			for (Category subCategory : subCategories) {
				removeCategory(subCategory);
			}
		}

		category.getSubCategories().removeAll(category.getSubCategories());
		category.getVocabularies().removeAll(category.getVocabularies());
		category.getKanjis().removeAll(category.getKanjis());
		category.getGrammars().removeAll(category.getGrammars());
		category.getSentences().removeAll(category.getSentences());
		commonServices.getCategoryDao().update(category);
		commonServices.getCategoryDao().delete(category);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action
	 * .IAction, org.eclipse.jface.viewers.ISelection)
	 */
	@Override
	public void selectionChanged(IAction action, ISelection selection) {

	}

}
